<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-10
 * Time: 17:55
 */

namespace app\wechatxcx\controller;

use app\wechatxcx\controller\Base;
use app\common\model\ChangguanYuyue;
use app\common\model\OrderGoodsclasses;
use app\service\PayService;
use app\common\model\Order as OrderModel;
use app\common\model\User as UserModel;
use EasyWeChat\Factory;

class Pay extends Base
{

    public function __construct()
    {
        parent::__construct();
    }



    /* 场地预定支付 */
    public function yuding()
    {

        $input = input();
        $order = ChangguanYuyue::getAll(array('order_sn' => array('=', $input['order_sn'])),'start_time asc');
        //需要在线支付
        $price = 0;
        $where = array();
        $where['checked'] = array('=', 1);
        foreach ($order as $k => $v) {
            $price += $v['price'];
            if($k==0){
                $where['start_time'] = array('>=', $v['start_time']);
            }else if($k==sizeof($order)-1){
                $where['end_time'] = array('<=', $v['end_time']);
            }
            $where['date'] = array('=', $v['date']);
            $where['cgid'] = $v['cgid'];
            $where['cdid'] = $v['cdid'];
        }
        $yiyuding = ChangguanYuyue::getAll($where);
        if($yiyuding){
            $this->ajax(205, '您选择的时段已被其他人预定了');

        }
        if($order[0]['checked']!=0){
            if($order[0]['checked']==1){
                $msg = '订单已支付';
            }else if($order[0]['checked']==2){
                $msg = '订单已取消';
            }else{
                $msg = '订单已退款';
            }
            $this->ajax(202, $msg);
        }


        if ($price > 0) {
            $orderConfig = [
                'body' => "场地预定",
                'out_trade_no' => $input['order_sn'],
                'total_fee' => $price * 100,
                // 'spbill_create_ip' => '123.12.12.123', // 可选，如不传该参数，SDK 将会自动获取相应 IP 地址
                'notify_url' => request()->domain() . "/notify/yuding",
                'trade_type' => 'JSAPI',
                //'openid' => UserModel::uid2openid($userInfo['id'])
                'openid' => $this->user['openid_main']
            ];

            $jsapi = PayService::S()->wechatJssdk($orderConfig);
            //$jsapi = SbPayService::S()->wechatJssdk($orderConfig);

            /* $data = [
              'appId' => $result['appid'],
              'timeStamp' => time(),
              'nonceStr' => $result['nonce_str'],
              'signType' => 'MD5',
              'paySign' => $result['sign'],
              'package' => "prepay_id={$result['prepay_id']}"
            ]; */
            $this->ajax(200, 'success', [
                'pay_money' => $price,
                'jsapi' => $jsapi
            ]);
        }

        $this->ajax(200, 'success', [
            'pay_money' => 0,
        ]);
    }

    /**
     * 场地预定退款
     * @return void
     */
    public function yudingrefund()
    {
        //查询条件
        $where = array(
            'order_sn' => input('order_sn'),
        );

        //预定信息
        $orderInfo = ChangguanYuyue::getAllGroupby($where,'order_sn','order_sn,sum(price) as pay_money,checked,start_time,date')[0];

        //预定信息存在
        if($orderInfo){
            $ordertime = strtotime($orderInfo['date'].' '.$orderInfo['start_time'])-3*24*60*60;
            if($ordertime<time()){
                $this->ajax(202, '取消订场需提前72小时', []);
            }

            if($orderInfo['checked'] != 1){

                $this->ajax(202, '订单未支付', []);
            }

            if($orderInfo['pay_money']<=0){
                $this->ajax(200, '退款成功', []);
            }else{

                $res = PayService::S()->wechatRefund($orderInfo);
            }

            //修改订单状态
            $save['checked'] = 3;
            ChangguanYuyue::update($save,$where);
            $this->ajax(200, '退款成功', []);

        }


        $this->ajax(202, '退款失败', []);
    }



    /* 充值 */

}